package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//菜品相关接口
@Api(tags = "菜品相关接口")
@Slf4j
@RestController
@RequestMapping("/admin/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result<String> save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}",dishDTO);
        int result = dishService.saveWithFlavor(dishDTO);
        if(result == StatusConstant.RETURN0){
            return Result.error(MessageConstant.FAIL_TO_ADD_DISH);
        }
        return Result.success();
    }
    /**
     * 分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("分页查询菜品")
    @GetMapping("/page")
    public Result<PageResult> pageQuery(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品:{}",dishPageQueryDTO);
        return Result.success(dishService.pageQuery(dishPageQueryDTO));
    }
    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @ApiOperation("根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> list(@ApiParam("分类id") Long categoryId){
        log.info("根据分类id查询菜品:{}",categoryId);
        return Result.success(dishService.getListByCategoryId(categoryId));
    }
    /**
     * 批量删除菜品
     * @param ids
     * @return
     */
    @ApiOperation("批量删除菜品")
    @DeleteMapping
//    RequestParam 交给spring框架解析出来封装进去
    public Result<String> delete(@ApiParam("菜品id") @RequestParam List<Long> ids){
        log.info("批量删除菜品:{}",ids);
        if(ids == null || ids.isEmpty()){
            return Result.error(MessageConstant.FAIL_TO_DELETE);
        }
        dishService.deleteBatch(ids);
        return Result.success();
    }
    /**
     * 根据id查询菜品数据
     * @param {id}
     * @return
     */
    @ApiOperation("根据id查询菜品数据")
    @GetMapping("/{id}")
    public Result<DishVO> getById(@ApiParam("菜品id") @PathVariable Long id){
        log.info("根据id查询菜品数据:{}",id);
        return Result.success(dishService.getDishVOById(id));
    }
    /**
     * 修改菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result<String> EditDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品:{}",dishDTO);
        dishService.EditDish(dishDTO);
        return Result.success();
    }
    /**
     * 菜品起售停售
     * @PathVariable status
     * @param id
     * @return
     */
    @ApiOperation("菜品起售停售")
    @PostMapping("/status/{status}")
    public Result<String> updateStatus(@ApiParam("菜品状态") @PathVariable Integer status,
                                       @ApiParam("菜品id") Long id){
        log.info("菜品起售停售:{}",id);
        dishService.updateStatus(status,id);
        return Result.success();
    }
}
